package fileoperations;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import commondata.IPlayer;
import commondata.ITradePackage;
import commondata.NbaPlayer;
import commondata.NbaPool;
import commondata.NbaTeam;
import commondata.TradePackage;

public class FileOperations implements IFileOperations {

	private static final int SEASON_GAMES = 82;

	private static final String TEAM_STATS_PATH = "./source/statistics/teams";

	private static int playerID = 0;

	private static int teamID = 0;

	private static String TOKEN = "+";
	
	private NbaPool instance = NbaPool.getInstance();
	
	private List<ITradePackage> tradePackages;

	private int getID() {
		return playerID++;
	}

	private int getTeamID() {
		return teamID++;
	}

//	private PlayerPositionEnum getPosition(String pos) {
//
//		if (pos.equalsIgnoreCase("C")) {
//			return PlayerPositionEnum.CENTER;
//		} else if (pos.equalsIgnoreCase("PG")) {
//			return PlayerPositionEnum.POINT_GUARD;
//		} else if (pos.equalsIgnoreCase("F")) {
//			return PlayerPositionEnum.POWER_FORWARD;
//		} else if (pos.equalsIgnoreCase("SF")) {
//			return PlayerPositionEnum.SMALL_FORWARD;
//		} else
//			return PlayerPositionEnum.SHOOTING_GUARD;
//	}

	private String getImageName(String name, String surname) {
		return "act_" + name + "_" + surname + ".jpg";
	}

	public void readStatistics() {

		tradePackages = instance.getTradePackages();
		BufferedReader br = null;
		String line = null;
		try {
			File f = new File(TEAM_STATS_PATH);
			if (!f.isDirectory())
				return;

			for (File file : f.listFiles()) {
				if (!file.getName().contains(".txt"))
					continue;

				br = new BufferedReader(new FileReader(file));
				line = br.readLine();
				StringTokenizer teamToken = new StringTokenizer(line);
				String teamName = teamToken.nextToken(TOKEN);
				String teamLogo = teamToken.nextToken(TOKEN);
				int teamWin = Integer.parseInt(teamToken.nextToken(TOKEN));
				int teamLoss = Integer.parseInt(teamToken.nextToken(TOKEN));

				NbaTeam team = new NbaTeam(getTeamID(), teamName, teamLogo,
						teamWin, teamLoss);
				List<Integer> ownedPlayerIDs = new ArrayList<Integer>();
				double teamPoints = 0;
				double teamReb = 0;
				double teamPayroll = 0;
				double teamAssists = 0;
				double teamBlocks = 0;
				double teamSteals = 0;
				while ((line = br.readLine()) != null) {
					IPlayer player = new NbaPlayer(getID());
					StringTokenizer st = new StringTokenizer(line);

					player.setJerseyNo(Integer.parseInt(st.nextToken(TOKEN)));
					player.setPlayerName(st.nextToken(TOKEN));
					player.setPlayerSurname(st.nextToken(TOKEN));

				
					//player.setPosition(getPosition(st.nextToken(TOKEN)));
					player.setPos(st.nextToken(TOKEN));
					player.setAge(Integer.parseInt(st.nextToken(TOKEN)));
					player.setHeight(st.nextToken(TOKEN));
					player.setWeight(Integer.parseInt(st.nextToken(TOKEN)));
					player.setCollege(st.nextToken(TOKEN));
					player.setWage(Double.parseDouble(st.nextToken(TOKEN)));
					player.setGamesPlyd(Integer.parseInt(st.nextToken(TOKEN)));
					player
							.setMinutesPlyd(Integer.parseInt(st
									.nextToken(TOKEN)));
					player.setMinutesPG();
					player.setFieldGoals(Integer.parseInt(st.nextToken(TOKEN)));
					player.setFieldGoalAttemps(Integer.parseInt(st
							.nextToken(TOKEN)));
					player.setFieldGoalsPer(Float.parseFloat(st
							.nextToken(TOKEN)));
					player.setFreeThrows(Integer.parseInt(st.nextToken(TOKEN)));
					player.setFreeThrowAttemps(Integer.parseInt(st
							.nextToken(TOKEN)));
					player.setFreeThrowsPer(Float.parseFloat(st
							.nextToken(TOKEN)));
					player
							.setThreePoints(Integer.parseInt(st
									.nextToken(TOKEN)));
					player.setThreePointAttemps(Integer.parseInt(st
							.nextToken(TOKEN)));
					player.setThreePointsPer();
					player
							.setOffRebounds(Integer.parseInt(st
									.nextToken(TOKEN)));
					player.setTotalRebounds(Integer.parseInt(st
							.nextToken(TOKEN)));
					player.setReboundPG();
					player.setAssists(Integer.parseInt(st.nextToken(TOKEN)));
					player.setAssistPG();
					player.setSteals(Integer.parseInt(st.nextToken(TOKEN)));
					player.setStealPG();
					player.setTurnovers(Integer.parseInt(st.nextToken(TOKEN)));
					// pergame
					player.setBlocks(Integer.parseInt(st.nextToken(TOKEN)));
					player.setBlockPG();
					player.setFouls(Integer.parseInt(st.nextToken(TOKEN)));
					// pergame
					player.setPoints(Integer.parseInt(st.nextToken(TOKEN)));
					player.setPointPG(Double.parseDouble(st.nextToken(TOKEN)));
					player.setRemainingYear(Integer.parseInt(st.nextToken(TOKEN)));
					player.setBYC(Boolean.parseBoolean(st.nextToken(TOKEN)));

					player.setImage(getImageName(player.getPlayerName(), player
							.getPlayerSurname()));

					teamPoints += player.getPoints();
					teamReb += player.getTotalRebounds();
					teamPayroll += player.getWage();
					teamAssists += player.getAssists();
					teamBlocks += player.getBlocks();
					teamSteals += player.getSteals();
					ownedPlayerIDs.add(player.getPlayerID());
					instance.getNbaPlayers().put(player.getPlayerID(), player);
					List<IPlayer> playerList = new ArrayList<IPlayer>();
					playerList.add(player);
					createTradePackage(playerList,team.getTeamID());

				}
				
				team.setOwnedPlayerIds(ownedPlayerIDs);
				team.setPointPG(teamPoints / SEASON_GAMES);
				team.setAssistPG(teamAssists / SEASON_GAMES);
				team.setBlockPG(teamBlocks / SEASON_GAMES);
				team.setReboundPG(teamReb / SEASON_GAMES);
				team.setStealPG(teamSteals/ SEASON_GAMES);
				team.setTeamPayroll(teamPayroll);
				createPackagesForTeam(team);
				instance.getPointsPG().add(team.getPointPG());
				instance.getReboundsPG().add(team.getReboundPG());
				instance.getAssistPG().add(team.getAssistPG());
				instance.getStealsPG().add(team.getStealPG());
				instance.getBlocksPG().add(team.getBlockPG());
				instance.getNbaTeams().put(team.getTeamID(), team);

			}
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		sortStatisticList();
		
	}

	private void sortStatisticList() {

		Collections.sort(instance.getPointsPG());
		Collections.sort(instance.getReboundsPG());
		Collections.sort(instance.getAssistPG());
		Collections.sort(instance.getStealsPG());
		Collections.sort(instance.getBlocksPG());
		
	}

	private void createPackagesForTeam(
			NbaTeam team) {

		List<Integer> teamSquad = team.getOwnedPlayerIds();
		int size = teamSquad.size();
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (i != j) {
					List<IPlayer> playerList = new ArrayList<IPlayer>();
					playerList.add(getPlayer(teamSquad.get(i)));
					playerList.add(getPlayer(teamSquad.get(j)));
					createTradePackage(playerList,team.getTeamID());
				}
			}
		}
	}
	
	private IPlayer getPlayer(int playerId){
		return instance.getNbaPlayers().get(playerId);
	}

	private void createTradePackage(List<IPlayer> playerList,int teamId) {

		double pointsPG = 0;
		double reboundPG = 0;
		double blockPG = 0;
		double stealPG = 0;
		double assistPG = 0;
		
		for (IPlayer pacplayer : playerList) {
			pointsPG += pacplayer.getPointPG();
			reboundPG += pacplayer.getReboundPG();
			blockPG += pacplayer.getBlockPG();
			stealPG += pacplayer.getStealPG();
			assistPG += pacplayer.getAssistPG();
		}
		int size = playerList.size();
		ITradePackage tradePackage = new TradePackage(playerList, pointsPG
				/ size, reboundPG / size, blockPG / size, assistPG / size,
				stealPG / size);
		tradePackage.setTeamID(teamId);
		tradePackages.add(tradePackage);
	}

	public static void main(String[] args) {
		new FileOperations().readStatistics();

	}

}
